package com.taxigang.server;
import java.io.UnsupportedEncodingException;
import java.util.Collection;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.allen_sauer.gwt.log.client.Log;
import com.taxigang.shared.AmbassadorResponseInfo;
import com.taxigang.shared.ContactInfo;
import com.taxigang.shared.LoginInfo;
import com.taxigang.shared.TaxiGangInfo;
import com.taxigang.shared.WishRouteInfo;

public class Mailer {

	private static Mailer instance = new Mailer();
	public static Mailer getInstance(){return instance;}
	
	public static void sendMessage(AmbassadorResponseInfo info){
		send(info);
	}
	
	public static void search(TaxiGangInfo info){
		String message = "SEARCH" ;
		send("hall@hall-services.de",message, info);
	}
	


	public static void sendMessage(String message, TaxiGangInfo info, LoginInfo loginInfo){
		Log.info("Mailer:sendMessage locale/country: "+ loginInfo.getLocale()+"/"+loginInfo.getCountry() );

		send("hall@hall-services.de",message, info);
		//if ("ro".equals(loginInfo.getLocale())){
			//send("eduard.encuna@gmail.com",message, info);
		//}
	}
	
	private static void send(String receiverEmail,String message, TaxiGangInfo info){
		Log.info("Mailer:"+receiverEmail+" : "+message);

        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = "From:"+info.getStart()+"\n";
        msgBody += "To:"+info.getDestination()+"\n";
        msgBody += "Time:"+info.getLocalDepartureTime()+"\n";
        msgBody += info.getLink();

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("peterredmondhall@gmail.com", "Taxigang"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress(receiverEmail, "Info"));
            msg.setSubject(message);
            msg.setText(msgBody);
            Transport.send(msg);
    
        } catch (AddressException e) {
            // ...
        } catch (MessagingException e) {
            // ...
	    } catch (UnsupportedEncodingException e) {
	        // ...
	    }

	}
	
	private static void send(AmbassadorResponseInfo info){

        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = "Name:"+info.name+"\n";
        msgBody += "Email:"+info.email+"\n";
        msgBody += "Handy:"+info.handy+"\n";
        msgBody += "Birthday:"+info.birthday;

        Log.info("Sending ambassador");

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("peterredmondhall@gmail.com", "Taxigang"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress("info@taxigang.com", "Info"));
            msg.setSubject("An ambassador response was received " );
            msg.setText(msgBody);
            Transport.send(msg);
    
        } catch (AddressException e) {
            Log.info(e.getMessage());
        } catch (MessagingException e) {
            Log.info(e.getMessage());
	    } catch (UnsupportedEncodingException e) {
            Log.info(e.getMessage());
	    }

	}

	public void sendContactInfo(ContactInfo info){

        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = "Name:"+info.getName()+"\n";
        msgBody += "Email:"+info.getEmail()+"\n";
        msgBody += "Handy:"+info.getMessage()+"\n";

        Log.info("Sending contact info");

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("peterredmondhall@gmail.com", "Taxigang"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress("info@taxigang.com", "Info"));
            msg.setSubject("A contact message was received " );
            msg.setText(msgBody);
            Transport.send(msg);
    
            //and now from info@taxigang.com
            msg.setFrom(new InternetAddress("info@taxigang.com", "Taxigang"));
            Transport.send(msg);

        } catch (AddressException e) {
            Log.info(e.getMessage());
        } catch (MessagingException e) {
            Log.info(e.getMessage());
	    } catch (UnsupportedEncodingException e) {
            Log.info(e.getMessage());
	    }

	}

	public void informWishRouteListeners(TaxiGangInfo taxiGangInfo, Collection<WishRouteInfo> wishRouteList) {
		for (WishRouteInfo wishRouteInfo : wishRouteList){
			send(taxiGangInfo,wishRouteInfo);
		}

		
	}

	private static void send(TaxiGangInfo taxiGangInfo, WishRouteInfo wishRouteInfo){
        Properties props = new Properties();
        Session session = Session.getDefaultInstance(props, null);

        String msgBody = invitationMessage(taxiGangInfo, wishRouteInfo);

        Log.info("Sending wish route info");

        try {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress("peterredmondhall@gmail.com", "Taxigang"));
            msg.addRecipient(Message.RecipientType.TO,
                             new InternetAddress(wishRouteInfo.getEmail(), "Join me!"));
            msg.setSubject("Join me!" );
            msg.setText(msgBody);
            Transport.send(msg);
    
        } catch (AddressException e) {
            Log.info(e.getMessage());
        } catch (MessagingException e) {
            Log.info(e.getMessage());
	    } catch (UnsupportedEncodingException e) {
            Log.info(e.getMessage());
	    }

	}

	public static String invitationMessage(TaxiGangInfo info, WishRouteInfo wishRouteInfo){
		String msgBody = info.getInvitationMessage();
		msgBody = msgBody.replace("***START***", info.getStart());
		msgBody = msgBody.replace("***DESTINATION***", info.getDestination());
		msgBody = msgBody.replace("***TIME***", info.getDepartureTime())+"\n\n";
		String link = "http://www.taxigang.com/?tg="+info.getId()+"&locale="+wishRouteInfo.getLocale();

		msgBody += link+"\n\n";
		msgBody += "To unsubscribe:\n";
		msgBody += "http://www.taxigang.com/?cancel="+wishRouteInfo.getId()+"&locale="+wishRouteInfo.getLocale();
		return msgBody;
	}
	


}
